import threading
import time
import urllib2, urllib
import socket
import random

portlist = []
suIP = []
global ff
ff = open('ipp.txt', 'a')


# pin--------------
def pin(IP):
    global ff
    socket.setdefaulttimeout(1.0)
    proxy_ip = {'http': "http://%s" % IP}
    proxy_support = urllib2.ProxyHandler(proxy_ip)
    opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)
    urllib2.install_opener(opener)
    url = 'http://baike.baidu.com/'
    socket.setdefaulttimeout(2.0)
    request = urllib2.Request(url)
    user_agent_list = [
        'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36',
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1",
        "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
    ]

    user_agent = random.choice(user_agent_list)
    request.add_header('User-Agent', user_agent)
    #print 'IP=%s' % IP
    try:
        html = urllib2.urlopen(request)
        print 'IP=%s' % IP
        ff.write(IP)
        ff.write('\n')
    except:
        # print IP
        pass


# --------------------------------
class timer(threading.Thread):  # The timer class is derived from the class threading.Thread
    def __init__(self, IP):
        threading.Thread.__init__(self)
        self.IP = IP
        self.thread_stop = False

    def run(self):  # Overwrite run() method, put what you want the thread do here
        threadLock = threading.Lock()
        threadLock.acquire()
        pin(self.IP)
        threadLock.release()


# ipp2=xxx.xxx.xxx-----------------
def d1(ipp2, por):
    threads1 = []
    for i in range(256):
        ipp1 = ipp2 + '.%s:%s' % (i, por)
        threads1.append(timer(ipp1))
    for t in threads1:
        t.start()
    for t in threads1:
        t.join()


# ipp3=xxx.xxx-------------------------
def d2(ipp3, por):
    threads2 = []
    for i in range(1):
        ipp2 = ipp3 + '.%s' % i
        threads2.append(threading.Thread(target=d1, args=(ipp2,por)))
    for t in threads2:
        t.start()
    for t in threads2:
        t.join()


# ipp4=xxx--------------------------------------

def d3(ipp4, por):
    threads3 = []
    for i in range(10):
        ipp3 = ipp4 + '.%s' % i
        threads3.append(threading.Thread(target=d2, args=(ipp3,por)))
    for t in threads3:
        t.start()
    for t in threads3:
        t.join()

def d3_2(ipp4, por):
    threads3 = []
    for i in range(1):
        ipp3 = ipp4 + '.%s' % i
        d2(ipp3,por)


def f3():
    # list1=[0,20,40,60,80,100,127,140,160,180,200,220,240,256]
    # list1=[0,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,105,110,115,120,125,130,135,140,145,150,155,160,165,170,175,180,185,190,195,200,205,210,215,220,225,230,235,240,245,250,256]
    for p in range(8000, 10000):
        for i in range(1, 256):
            time.sleep(10)
            print i
            d3(str(i), p)


def prots():
    f3()


if __name__ == '__main__':
    print 'aa'
    prots()
